package tkM

import (
	"github.com/panjf2000/ants/v2"
	"db2s/Er"
	"db2s/Meta"
	metaD "db2s/MetaInit"
	"db2s/arg"
	cp "db2s/componentInit"
	"db2s/conn"
	"db2s/global"
	"db2s/outPut"
	"db2s/parDef"
	"db2s/pubM"
	quit "db2s/quitDispos"
	mq "db2s/topic-mq"
	"os"
	"reflect"
	"sync"
	"time"
)

type (
	schemaInitContext SchemaInitContext
	// Topic 自定义Topic 类型 限制范围 避免出错
	Topic string
	Logo  string //dataFix Type 标识
	// CheckObject 自定义CheckObject 类型 限制范围 避免出错
	CheckObject string
	CheckMode   string // CheckMode
	IndexType   string //Index type distinction
	Keywords    string //data split keyword
)

const (
	taskBaseQuery      Topic = "taskBaseQuery"
	taskStructQuery    Topic = "taskStructQuery"
	taskPartitionQuery Topic = "taskPartitionQuery"
	taskIndexQuery     Topic = "taskIndexQuery"
	taskHistogramQuery Topic = "taskHistogramQuery"
	taskIndexChoose    Topic = "taskIndexChoose"
)

var (
	lock            sync.Mutex
	SchemaContext   *schemaInitContext
	PlanContext     *SchedulePlanContext
	GlobalPConfigs  *GlobalParametersConfig
	GlobalISObjects *GlobalISObject
	closeObject     quit.Close
	tableObjectFile map[string]*os.File
	//producer 生产者
	producer mq.Producer
	//consumer 消费者
	consumer                mq.Consumer
	FuncRef                 map[string]reflect.Value
	ScheduleTaskInitFuncRef map[string]reflect.Value
	MqListenerSInitFunc     map[string]reflect.Value
	//WLog                    *log.Logger
)

type (
	SchemaInitContextConfig struct {
		TableList     []string            //表对象清单
		GtCheckConfig arg.ConfigParameter //客户端传参
	}
	// GlobalISObject 全局表结构或结构初始化对象
	GlobalISObject struct {
		TableMetaData map[string]Er.TablesMetaInfoEr
		tableMetaEr   map[string]Meta.TableEr
	}
	ReadWriteFilePointS struct {
		ParameterRecord *os.File
		BreakPointFin   *os.File
		ActiveDataFin   *os.File
		TtyBarFin       *os.File
		ObjectResult    *os.File
		ObjectMiss      *os.File //表对象缺失
	}
	Conn struct {
		Source conn.ConEr
		Target conn.ConEr
	}
	GlobalParametersConfig struct { // SchedulePlanConfig 输入参数重新复制
		TableList      []string
		InstanceConfig global.DBInstanceConfig
		FilePoint      ReadWriteFilePointS
		TableObject    []*arg.InputTableAttributes
		argPar         arg.ArgInput
		repair         arg.RepairS
		result         arg.Result
		rules          arg.RulesS
		dSns           arg.DSNsS
		DBPool         arg.DBPool
		structs        arg.StructS
		db             pubM.Conn
	}
	CheckSchemaMetaData struct {
		Total                 *int64
		TableObject           chan arg.InputTableAttributes
		TableObjectFlow       chan any
		ObjectSum             int
		IgnoreObjectSum       int64
		schemaMetaMsgSendBody *mq.MonitorMsgMq
	}
	SchedulePlanContextConfig struct {
		TableList     []string
		BeginTime     time.Time
		GtCheckConfig arg.ConfigParameter
		MQConfig      *cp.MQConfig
	}
	ObjectRoleInfo struct {
		Schema     string
		Table      string
		MSchema    string
		MTable     string
		ObjectRole string //对象角色
	}
	columnHistogram struct {
		Name      string //索引名
		Column    []string
		Total     *int64   //总数
		Segment   *int64   //段数
		Break     *int64   //中止
		UniqueSum []*int64 //唯一数
		AllColumn []string //所有列
		Histogram float64  //离散度%百分比
	}
	columnHistogramMerge struct {
		Name      string   //索引名
		Column    []string //索引列
		Type      string   //索引类型
		Total     *int64   //总行数
		Segment   *int64
		UniqueSum []*int64
	}
	// SchedulePlan2 执行计划操作对象
	SchedulePlan2 struct {
		Object                     global.Object
		SrcParameter, DstParameter parDef.Parameter
		TableTargetStatus          bool
		MQ                         *mq.MonitorMsgMq
		ReadOptimizer              global.ReadOptimizer
		repair                     arg.RepairS
		rules                      arg.RulesS
		writeOptimizer             global.WriteOptimizer
		TableLocNumber             uint64
		TableSpeedLimit            global.TableSpeedLimit
		Status                     global.TableStatus
		subTaskInfo                *mq.SubTasks
		TPod                       *outPut.TPod //终端输出结果
		XlsTerminal                *outPut.WorkSheetTerminal
		XlsResult                  *outPut.WorkSheetResult
		XlsMiss                    *outPut.WorkSheetMiss
		//Task                     global.TaskPlan
		mqProductCustomerMonitor global.MqProductCustomerMonitorMessage
	}
	TableObjectMiss struct {
		Seq          int
		ObjectLogSeq int64
		ObjectName   string
		Schema       string
		Table        string
	}
	// SchedulePlanContext 执行计划上下文
	SchedulePlanContext struct {
		//多任务执行计划
		ChanSchedulePlanTable chan any //[]map[string] /*SchemaToTable*/ *SchedulePlan2
		//存储执行计划任务
		SchedulePlanTable any //map[string] /*SchemaToTable*/ *SchedulePlan2
		// 上下文任务线程池
		Pool *ants.Pool
		//自动关闭mq的组件
		AutoCloseMqComponentTable map[cp.CheckMode]*cp.AutoCloseMqComponent
		//进度条
		TaskBarTable *mq.TaskBar
		Rw           sync.Mutex
		BeginTime    time.Time
		wg           sync.WaitGroup
	}
	// SchemaInitContext 执行计划上下文
	SchemaInitContext struct {
		Seq               int
		Schema            string
		Table             string
		MSchema           string
		MTable            string
		ObjectRole        string
		ObjectLogSeq      int64
		TColumnMeta       global.TableMetaGather
		IndexData         metaD.IndexChooseResult
		TableSum          int
		IgnoreObjectSum   int64
		TableFree         *int64
		TableAccount      *int64
		BreakPointSum     *int64
		TablePartition    global.PartitionGarth
		ChanSchemaContext chan any
		TPod              *outPut.TPod
	}
	AutoCloseMqComponent struct {
		TopicStatistics  map[Topic]*int64
		UnsubscribeTopic []Topic
		CheckMode        CheckMode
		EndMark          bool
	}
)
